package org.bookr.engine.service.impl;

import org.bookr.engine.dao.BookrDao;
import org.bookr.engine.model.*;
import org.bookr.engine.service.BookrService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Pawel Tomaszewski
 */
public class BookrServiceImpl implements BookrService {
    protected BookrDao bookrDao;

    public List<Book> fetchRandomBooks(Integer count) {
        Set<Book> books = bookrDao.getBooks();
        List<Book> results = new ArrayList<Book>(books);
        Collections.shuffle(results);
        return results.subList(0, count);
    }

    public List<Administrator> listAdministrators() {
        return new ArrayList<Administrator>(bookrDao.getAdministrators());
    }

    public List<Author> listAuthors() {
        return new ArrayList<Author>(bookrDao.getAuthors());
    }

    public List<Book> listBooks() {
        return new ArrayList<Book>(bookrDao.getBooks());
    }

    public List<Category> listCategories() {
        return new ArrayList<Category>(bookrDao.getCategories());
    }

    public List<Comment> listComments() {
        return new ArrayList<Comment>(bookrDao.getComments());
    }

    public List<Group> listGroups() {
        return new ArrayList<Group>(bookrDao.getGroups());
    }

    public List<Reader> listReaders() {
        return new ArrayList<Reader>(bookrDao.getReaders());
    }

    public AuthenticablePerson loadUserByUsername(String username) {
        AuthenticablePerson authenticablePerson = bookrDao.getAdministatorByUsername(username);
        if(authenticablePerson == null) {
            authenticablePerson = bookrDao.getReaderByUsername(username);
        }
        return authenticablePerson;
    }

    public Administrator loadAdministratorById(String id) {
        return bookrDao.getAdministratorById(id);
    }

//    public Administrator loadAdministratorByUsername(String username) throws UsernameNotFoundException {
//        return null;  //To change body of implemented methods use File | Settings | File Templates.
//    }

    public Author loadAuthor(String id) {
        return bookrDao.getAuthor(id);
    }

    public Book loadBook(String id) {
        return bookrDao.getBook(id);
    }

    public Category loadCategory(String id) {
        return bookrDao.getCategory(id);
    }

    public Comment loadComment(String id) {
        return bookrDao.getComment(id);
    }

    public Group loadGroup(String id) {
        return bookrDao.getGroup(id);
    }

    public Reader loadReaderByUsername(String username) throws UsernameNotFoundException {
        return bookrDao.getReaderByUsername(username);
    }

    public Reader loadReaderById(String id) {
        return bookrDao.getReaderById(id);
    }

    public String updateAdministrator(Administrator administrator) {
        return bookrDao.updateAdministrator(administrator);
    }

    public String updateAuthor(Author author) {
        return bookrDao.updateAuthor(author);
    }

    public String updateBook(Book book) {
        return bookrDao.updateBook(book);
    }

    public String updateCategory(Category category) {
        return bookrDao.updateCategory(category);
    }

    public String updateComment(Comment comment) {
        return bookrDao.updateComment(comment);
    }

    public String updateGroup(Group group) {
        return bookrDao.updateGroup(group);
    }

    public String updateReader(Reader reader) {
        return bookrDao.updateReader(reader);
    }

    public void removeAdministrator(Administrator administrator) {
        bookrDao.deleteAdministrator(administrator);
    }

    public void removeAuthor(Author author) {
        bookrDao.deleteAuthor(author);
    }

    public void removeBook(Book book) {
        bookrDao.deleteBook(book);
    }

    public void removeCategory(Category category) {
        bookrDao.deleteCategory(category);
    }

    public void removeComment(Comment comment) {
        bookrDao.deleteComment(comment);
    }

    public void removeGroup(Group group) {
        bookrDao.deleteGroup(group);
    }

    public void removeReader(Reader reader) {
        bookrDao.deleteReader(reader);
    }


    public BookrDao getBookrDao() {
        return bookrDao;
    }

    public void setBookrDao(BookrDao bookrDao) {
        this.bookrDao = bookrDao;
    }
}
